package com.kuaimacode.kframework.admin.controller.sys;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.services.sys.SysMessageService;
import com.kuaimacode.kframework.admin.vo.request.SysMessageReq;
import com.kuaimacode.kframework.admin.vo.response.SysMessageResp;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.exception.ErrorEnum;
import com.kuaimacode.kframework.exception.ServerInternalErrorException;
import com.kuaimacode.kframework.mybatis.models.sys.SysMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author Administrator
 * @date 2019/03/31
 */

/**
 * Controller
 */
@Controller
@RequestMapping({"/message"})
public class SysMessageController {
    private static String PAGE_FILE_NAME = "message/";

    @Autowired
    private SysMessageService messageServices;


    @RequestMapping(value = {"/index"})
    public String index(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM + "") int pageNum,
                        @RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE + "") int pageSize,
                        ModelMap model, SysMessage searchMessage) {
        PageInfo<SysMessageResp> pageInfo = messageServices.findAll(pageNum, pageSize, searchMessage);
        // pageInfo.list message
        model.put("pageInfo", pageInfo);
        return PAGE_FILE_NAME + "index";
    }


    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap map) {
        SysMessage message = new SysMessage();
        map.put("message", message);
        map.put("isAdd", true);
        return PAGE_FILE_NAME + "form";
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable String id, ModelMap map) {
        SysMessage message = messageServices.getById(id);
        map.put("message", message);
        map.put("isAdd", false);
        return PAGE_FILE_NAME + "form";
    }

    @RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult edit(@Valid SysMessageReq message, Boolean isAdd, ModelMap map) {
        try {
            if (isAdd) {
                // 新建保存
                if (StringUtils.isBlank(message.getPhone())) {
                    throw new ServerInternalErrorException(ErrorEnum.PARAM_VALID_ERROR);
                }
                messageServices.save(message);
            } else {
                if (message.getUserId() == null || message.getId() == null) {
                    throw new ServerInternalErrorException(ErrorEnum.PARAM_VALID_ERROR);
                }
                // 编辑保存
                SysMessage updateMsg = new SysMessage();
                BeanUtils.copyProperties(message, updateMsg);
                messageServices.updateById(updateMsg);
            }
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }


    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public String detail(@PathVariable String id, ModelMap map) {
        SysMessage message = messageServices.getById(id);
        map.put("message", message);
        return PAGE_FILE_NAME + "detail";
    }


    @ResponseBody
    public JsonResult delete(@PathVariable String id, ModelMap map) {
        try {
            messageServices.removeById(id);
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }
}
